from threading import Thread
import pyqtgraph as pg
import MySQLdb, re
import icon
import requests
from PySide6 import QtCore
from PySide6.QtGui import QIcon, QFontDatabase, QFont
from PySide6.QtWidgets import (QHBoxLayout, QVBoxLayout, QApplication, QFrame, QLabel, QPushButton, QPlainTextEdit,
                               QMainWindow, QWidget, QLineEdit, QFormLayout, QGridLayout, QTextEdit, QMessageBox,
                               QComboBox, QTableWidget, QTableWidgetItem, QSizePolicy, QDateEdit, QMenuBar, QToolBar,
                               QStyle, QFileDialog, QDockWidget, QMdiArea, QToolButton, )
from PySide6.QtCore import Qt, QSize, QObject, Signal, QDate
import os


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        main_window = QWidget(self)
        self.setCentralWidget(main_window)

        self.resize(600, 400)
        self.main_title = '未设置项目'
        self.setWindowTitle(f'接口压测工具 - {self.main_title}')
        self.setWindowIcon(QIcon('stress_test.jpeg'))
        self.setMenuBar(Menubar(self))  # 设置菜单栏
        self.menuBar().setNativeMenuBar(False)  # 禁用掉原生菜单栏，iOS原生菜单栏是在整个屏幕窗口的
        self.addToolBar(Tool(self))  # 设置工具栏
        self.log_win1 = LogWindow(self)  # 设置日志窗口
        self.addDockWidget(Qt.BottomDockWidgetArea, self.log_win1)  # 添加到底部
        main_hl = QHBoxLayout(main_window)

        frame_icon = QFrame()  # QFrame 是一个基础的容器控件，可以用来创建边框、分隔线或作为其他控件的容器
        # frame_icon.setFrameShape(QFrame.Shape.StyledPanel)  # 设置边框样式
        # frame_icon.setFrameShadow(QFrame.Shadow.Raised)  # 设置边框阴影
        #frame_icon.setLineWidth(2)  # 设置边框宽度
        main_hl.addWidget(frame_icon)
        frame_vl = QVBoxLayout(frame_icon)  # 给侧边容器控件添加一个垂直布局
        frame_vl.setContentsMargins(0, 10, 0, 0)
        # frame_vl.setSpacing(0)
        main_hl.addLayout(frame_vl)  # 把侧边容器布局添加到主布局中
        # 添加系统按钮图标
        # self.info_button = QToolButton()
        # self.info_button.setIcon(self.style().standardIcon(getattr(QStyle.StandardPixmap, 'SP_ComputerIcon')))  # 使用系统主题图标
        # self.info_button.setIconSize(QSize(28, 28))
        # self.info_button.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonTextUnderIcon)
        # self.info_button.setToolTip('我的电脑')
        # frame_vl.addWidget(self.info_button)
        # 添加FontAwesome类型图标\
        # 使用FontAwesome中的图标添加按钮
        current_dir = os.path.dirname(os.path.abspath(__file__))  # 获取当前目录
        font_file = os.path.join(current_dir, 'Font Awesome 5 Free-Solid-900.otf')  # 获取文字的路径
        font_id = QFontDatabase.addApplicationFont(font_file)  # 获取文字字体ID
        families = QFontDatabase.applicationFontFamilies(font_id)  # 获取字体系列名称
        fa_family = families[0]  # 通常第一个就是我们要的字体家族名
        font = QFont(fa_family)  # 创建一个字体的对象
        font.setPixelSize(36)  # 设置图标大小

        # 设置按钮的样式，颜色查询  https://www.qtccolor.com/secaiku/search?word=f
        color_icon = "#41678D"
        color_background = '#d9e046'
        color_hover = '#FFFF00'    #
        color_pressed = '#e09d46'   # 橙色
        button_style = f'''
            QPushButton {{
                color: {color_icon};           /* 默认图标颜色 */
                border: none;           /* 无边框 */
            }}
            QPushButton:hover {{ 
                background-color: {color_hover};  /* 悬停 */
            }}
            QPushButton:pressed {{  
                background-color: {color_pressed};   /* 按下 */
            }}
                        '''

        self.button_client = QPushButton('\uf007')
        self.button_client.setFlat(True)
        self.button_client.setFont(font)  # 将按钮按钮设置 FontAwesome 字体
        self.button_client.setToolTip('客户端')  # 设置悬浮按钮时候展示的文字
        self.button_client.setStyleSheet(button_style)
        frame_vl.addWidget(self.button_client)

        self.button_user = QPushButton('\uf0c0')
        self.button_user.setFlat(True)
        self.button_user.setFont(font)  # 将按钮按钮设置 FontAwesome 字体
        self.button_user.setToolTip('用户')  # 设置悬浮按钮时候展示的文字
        self.button_user.setStyleSheet(button_style)
        frame_vl.addWidget(self.button_user)

        self.button_stress = QPushButton('\uf496')
        self.button_stress.setFlat(True)
        self.button_stress.setFont(font)  # 将按钮按钮设置 FontAwesome 字体
        self.button_stress.setToolTip('压力测试')  # 设置悬浮按钮时候展示的文字
        self.button_stress.setStyleSheet(button_style)
        frame_vl.addWidget(self.button_stress)

        self.button_monitor = QPushButton('\uf109')
        self.button_monitor.setFlat(True)  # 移除边框
        self.button_monitor.setFont(font)  # 将按钮按钮设置 FontAwesome 字体
        self.button_monitor.setToolTip('实时监控')  # 设置悬浮按钮时候展示的文字
        self.button_monitor.setStyleSheet(button_style)
        frame_vl.addWidget(self.button_monitor)

        self.button_help = QPushButton('\uf128')
        self.button_help.setFlat(True)
        self.button_help.setFont(font)  # 将按钮按钮设置 FontAwesome 字体
        self.button_help.setToolTip('帮助')  # 设置悬浮按钮时候展示的文字
        self.button_help.setStyleSheet(button_style)
        frame_vl.addWidget(self.button_help)

        mdi_area = QMdiArea()  # 添加一个MDI窗口
        main_hl.addWidget(mdi_area)

    # 菜单栏


class Menubar(QMenuBar):
    def __init__(self, parent=None):
        super().__init__(parent)
        # 添加菜单栏的按钮
        file_menu = self.addMenu('文件')
        test_menu = self.addMenu('测试')
        # 添加按钮中的行动点
        open_action = file_menu.addAction('打开文件目录')

        # 连接信号
        file_menu.triggered.connect(self.on_open)  # 打开文件

    def on_open(self):
        file = QFileDialog.getExistingDirectory(self, "选择存储路径")  # 打开文件目录
        # QFileDialog.getOpenFileName(
        #     self,
        #     '选择打开文件'
        # )   # 打开已经存在的文件
        # file = QFileDialog.getSaveFileName(
        #     self,
        #     '保存文件'
        # )
        win1 = self.parent().window()  # 获取父类的窗口
        win1.main_title = file
        win1.setWindowTitle(f'接口压测工具 - {win1.main_title}')


# 工具栏
class Tool(QToolBar):
    def __init__(self, parent=None):
        super().__init__(parent)
        style = self.style()  # 获取原生的图标样式
        open_icon = style.standardIcon(QStyle.SP_DialogOpenButton)  # 设置文件的图标样式
        self.addAction(open_icon, "文件").triggered.connect(self.on_open)

    def on_open(self):
        file = QFileDialog.getExistingDirectory(self, "选择存储路径")
        win2 = self.parent().window()  # 获取父类的窗口
        win2.main_title = file  # 设置主窗口的标题
        win2.setWindowTitle(f'接口压测工具 - {win2.main_title}')


# 日志输出窗口
class LogWindow(QDockWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle('日志展示')
        self.setObjectName("LogWindow")  # 必须设置用于保存布局
        self.log_win = QPlainTextEdit()
        self.log_win.setReadOnly(True)  # 设置为只读模式
        # 允许拖动
        self.setFeatures(
            QDockWidget.DockWidgetMovable |
            QDockWidget.DockWidgetFloatable
        )
        # 设置允许拖动的位置
        self.setAllowedAreas(
            Qt.BottomDockWidgetArea |
            Qt.RightDockWidgetArea
        )
        log = QWidget()  # 创建一个中间控件
        log_v = QVBoxLayout()
        log_v.addWidget(self.log_win)
        log.setLayout(log_v)
        self.setWidget(log)


app = QApplication()
win = MainWindow()
win.show()
app.exec()
